<?php defined('BASEPATH') OR exit('No direct script access allowed');

class File extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('file_m');
        $this->load->library('pagination');
    }

    /**
     * 列表
     * [index description]
     * @return [type] [description]
     */
    public function index($page = 1)
    {

        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->db->count_all('file');
        $config['base_url']         = site_url('manage/file/index/');
        $config['per_page']         = $limit;
        $config['num_links']        = 10;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $files              = $this->file_m->get_all_files($start, $limit);
        $data['files'] = $files;
        $this->load->view('files/index', $data);
    }

    /**
     * 新增
     * [add description]
     */
    public function add()
    {
        $data['step']     = 1;
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
//        pp($data);exit();
        $this->load->view('files/form', $data);
    }

    /**
     * 修改文件
     * @param     $file_id
     * @param int $step
     */
    public function edit($file_id, $step = 1)
    {
        if ($file_id && $step) {
            $file = $this->file_m->get_file_by_id($file_id);
            // pp($file);
            // 获取分类
            $data['file']           = $file;
            $data['file_id']        = $file_id;
            $data['file_real_name'] = $file['file_real_name'];
            $data['file_location']  = $file['file_location'];
            $data['step']           = $step;
            // 商品图片
            if (!empty($file['file_location'])) {

                $pic_path = "[";
                $pic_con  = [];
                $del_url  = base_url("index.php/manage/pubfun/delete_file");;
                $pic_path         .= "'" . $file['file_location'] . "',";
                $pic_con[]        = [
                    'caption' => $file['file_name'],
                    'url'     => $del_url,
                    "key"     => $file['id'],
                ];
                $data['pic_path'] = $pic_path . "]";
                $data['pic_con']  = json_encode($pic_con);
            } else {
                $data['pic_path'] = "[]";
                $data['pic_con']  = json_encode([]);
            }
            // pp($data);exit();
            $this->load->view('files/form', $data);
        } else {
            show_message('参数错误', site_url(X_URL . 'file/index'), 1);
        }
    }

    /**
     * 文件信息
     * [update description]
     * @return [type] [description]
     */
    public function update()
    {

        $post    = $this->input->post();
        $file_id = isset($post['file_id']) ? intval($post['file_id']) : false;
        // pp($file_id);exit;
        $file_title    = isset($post['file_name']) ? htmlspecialchars($post['file_name'], ENT_QUOTES) : false;
        $file_location = isset($post['file_location']) ? $post['file_location'] : false;
        if (!$file_id) {
            // 新增操作，先添加用户，再添加文件信息
            $file  = [
                'file_name'      => $file_title,
                'file_real_name' => $post['file_real_name'],
                'file_location'  => $file_location,
                'update_time'    => date("Y-m-d H:i:s"),
            ];
            $files = $this->file_m->add_file($file);
            if ($files) {
                show_message('新增文件成功', site_url(X_URL . 'file/index'), 1);
            } else {
                show_message('新增文件失败', site_url(X_URL . 'file/edit/'), 1);
            }
        } else {
            // 修改文件信息
            $file          = [
                'file_name'      => $file_title,
                'file_real_name' => $post['file_real_name'],
                'file_location'  => $file_location,
                'update_time'    => date("Y-m-d H:i:s"),
            ];
            $affected_rows = $this->file_m->sava_file($file_id, $file);
            if ($affected_rows) {
                show_message('修改文件成功', site_url(X_URL . 'file/index'), 1);
            } else {
                show_message('修改文件失败', site_url(X_URL . 'file/edit'), 1);
            }
        }
    }
}
